home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 April: Mac OS SDK / Dev.CD Apr 00 SDK1.toast / Development Kits / Mac OS / Open Transport 1.3 / Open Transport SDK / Open Tpt Client Developer / Samples / Internet / OTTcpPitchSample.cp < prev    next >
Encoding:
Text File  |  1998-04-30  |  10.5 KB  |  469 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OTTcpPitchSample.cp
  3.  
  4.     Contains:    Tcp pitch sample.
  5.  
  6.     Copyright:    © 1993-1997 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10.  
  11. // OT TCP Pitch Test Program (as an SIOW app)
  12.  
  13. #include <QuickDraw.h>
  14. #include <stdio.h>
  15. #include <StdLib.h>
  16. #include <TextUtils.h>
  17. #include <strings.h>
  18. #include <String.h>
  19. #include <Events.h>
  20. #include <Menus.h>
  21. #include <Devices.h>
  22. #include <OpenTransport.h>
  23. #include <OpenTptInternet.h>
  24.  
  25. #ifndef T_DISCON_IND
  26. #define T_DISCON_IND 128
  27. #endif
  28.  
  29.  
  30. /*******************************************************************************
  31. ** GLOBAL VARIABLES
  32. ********************************************************************************/
  33.  
  34. #define kMaxDataLen 256
  35.  
  36. const size_t    kMyPoolSize = 60000;
  37.  
  38. InetPort    gCatchPort        = 0;
  39. InetHost    gCatchIpAddr    = 0;
  40. InetPort      gPitchPort        = 0;
  41. InetHost    gPitchIpAddr    = 0;
  42.  
  43. unsigned short gBindCompleted        = 0;
  44. unsigned short gCallRcvOrdDiscon    = 0;
  45.  
  46. struct InetAddress sndsin, rcvsin, reqsin, retsin;
  47. char data[kMaxDataLen];
  48. char defaultData[] = "Go Cal, beat Stanford !!!";
  49.  
  50. /*******************************************************************************
  51. ** Function Prototypes
  52. ********************************************************************************/
  53.  
  54. void Inits();
  55. void CleanUp();
  56. void Idle();    
  57. void DoIt();
  58.  
  59.  
  60. /*******************************************************************************
  61. **  main function
  62. ********************************************************************************/
  63.  
  64. void main()
  65. {
  66.     Inits();
  67.     DoIt();
  68.     CleanUp();
  69. }
  70.  
  71. /*******************************************************************************
  72. ** Initialize Quickdraw and ASLM
  73. ********************************************************************************/
  74.  
  75. void Inits()
  76. {
  77.     InitGraf(&qd.thePort);
  78.     if (InitOpenTransport() != kOTNoError)
  79.     {
  80.         fprintf(stderr, "OTTcpPitch: Could not initialize ASLM, exiting\n");
  81.         exit(1);
  82.     }
  83. }
  84.  
  85. /*******************************************************************************
  86. ** ShowEndpointOptions
  87. ********************************************************************************/
  88.  
  89. void ShowEndpointOptions(TEndpoint*    ep)
  90. {
  91.     /*
  92.      *    This function retrieves and displays 
  93.      *    the IP and TCPP endpoint options for this endpoint.
  94.      */
  95.      
  96.     OSStatus    err;
  97.     TOptMgmt*            ret = (TOptMgmt*)OTAlloc(ep, T_OPTMGMT, T_OPT, &err);
  98.  
  99.     
  100.     do
  101.     {
  102.         fprintf(stderr, "Current Readable IP Option Settings for endpoint @ %08lX:\n", ep);
  103.         if ( ret == NULL )
  104.         {
  105.             fprintf(stderr, "ERROR: could not allocate TOptMgmt structure (%d)\n", err);
  106.             break;
  107.         }
  108.         //
  109.         // Get the current IP options
  110.         //
  111.         TOptMgmt        req;
  112.         TOptionHeader    option;
  113.         
  114.         option.len        = kOTOptionHeaderSize;
  115.         option.level    = INET_IP;
  116.         option.name        = T_ALLOPT;
  117.         
  118.         req.opt.buf = (UInt8*)&option;
  119.         req.opt.len    = kOTOptionHeaderSize;
  120.         req.flags    = T_CURRENT;
  121.         
  122.         err = ep->OptionManagement(&req, ret);
  123.         if ( err != kOTNoError )
  124.         {
  125.             fprintf(stderr, "ERROR: OptionManagement T_CURRENT request returned %d\n", err);
  126.             break;
  127.         }
  128.         //
  129.         // Now, let's print the options
  130.         //
  131.         {
  132.             TOption*    opt = (TOption*)ret->opt.buf;
  133.             char        string[512];
  134.  
  135.             err = OTCreateOptionString("ip", &opt, ret->opt.buf + ret->opt.len,
  136.                                        string, sizeof(string));
  137.             
  138.             if ( err == kOTNoError )
  139.             {
  140.                 char*    str = string;
  141.                 size_t    len = 0;
  142.                 while ( true )
  143.                 {
  144.                     char* temp = strchr(str, ',');
  145.                     if ( temp == NULL )
  146.                     {
  147.                         fprintf(stderr, "%s\n", str);
  148.                         break;
  149.                     }
  150.                     if ( len + temp - str + 1 > 80 )
  151.                     {
  152.                         fprintf(stderr, "\n");
  153.                         if ( *str == ' ' )
  154.                             str += 1;
  155.                         len = 0;
  156.                     }
  157.                     fprintf(stderr, "%*.*s", temp - str + 1, temp - str + 1, str);
  158.                     len += temp - str + 1;
  159.                     str = temp + 1;
  160.                 }
  161.             }
  162.         }
  163.         
  164.         TOption* opt =  OTFindOption(ret->opt.buf, ret->opt.len, INET_IP, IP_TTL);
  165.             
  166.         if ( opt == NULL )
  167.             fprintf(stderr, "ERROR:OptionManagement did not have IP_TTL in returned options\n");
  168.  
  169.         //
  170.         // Get the current TCP options
  171.         //
  172.         fprintf(stderr, "Current Readable TCP Option Settings for endpoint @ %08lX:\n", ep);
  173.         option.len        = kOTOptionHeaderSize;
  174.         option.level    = INET_TCP;
  175.         option.name        = T_ALLOPT;
  176.         
  177.         req.opt.buf = (UInt8*)&option;
  178.         req.opt.len    = kOTOptionHeaderSize;
  179.         req.flags    = T_CURRENT;
  180.         
  181.         err = ep->OptionManagement(&req, ret);
  182.         if ( err != kOTNoError )
  183.         {
  184.             fprintf(stderr, "ERROR: OptionManagement T_CURRENT request returned %d\n", err);
  185.             break;
  186.         }
  187.         //
  188.         // Now, let's print the options
  189.         //
  190.         {
  191.             TOption*    opt = (TOption*)ret->opt.buf;
  192.             char        string[512];
  193.  
  194.             err = OTCreateOptionString("tcp", &opt, ret->opt.buf + ret->opt.len,
  195.                                        string, sizeof(string));
  196.             
  197.             if ( err == kOTNoError )
  198.             {
  199.                 char*    str = string;
  200.                 size_t    len = 0;
  201.                 while ( true )
  202.                 {
  203.                     char* temp = strchr(str, ',');
  204.                     if ( temp == NULL )
  205.                     {
  206.                         fprintf(stderr, "%s\n", str);
  207.                         break;
  208.                     }
  209.                     if ( len + temp - str + 1 > 80 )
  210.                     {
  211.                         fprintf(stderr, "\n");
  212.                         if ( *str == ' ' )
  213.                             str += 1;
  214.                         len = 0;
  215.                     }
  216.                     fprintf(stderr, "%*.*s", temp - str + 1, temp - str + 1, str);
  217.                     len += temp - str + 1;
  218.                     str = temp + 1;
  219.                 }
  220.             }
  221.             fprintf(stderr, "\n");
  222.         }
  223.         
  224.     } while ( false );
  225.     OTFree(ret, T_OPTMGMT);
  226. }
  227.  
  228.  
  229. /*******************************************************************************
  230. ** Clean up at the end
  231. ********************************************************************************/
  232.  
  233. void CleanUp()
  234. {
  235.     CloseOpenTransport();
  236. }
  237.  
  238. /*******************************************************************************
  239. ** Idle
  240. ********************************************************************************/
  241.  
  242. void Idle()
  243. {
  244.     SystemTask();
  245. }
  246.  
  247. /*******************************************************************************
  248. ** EventHandler
  249. ********************************************************************************/
  250.  
  251. pascal void EventHandler(void*, OTEventCode event, OTResult, void*)
  252. {
  253.     OTEventCode tempevent = 0;
  254.  
  255.     switch ( event )
  256.     {
  257.         case T_BINDCOMPLETE:
  258.                             gBindCompleted = 1;
  259.                             break;
  260.         case T_ORDREL:
  261.                             gCallRcvOrdDiscon = 1;
  262.                             break;
  263.         default:
  264.                             DebugStr("\pEventHandler got unexpected event");
  265.                             break;
  266.     }
  267.     return;
  268. }
  269.  
  270. /*******************************************************************************
  271. ** DoIt
  272. ********************************************************************************/
  273.  
  274. void DoIt()
  275. {
  276.     TEndpoint*        ep = NULL;
  277.     TEndpointInfo    info;
  278.     TBind            req, ret;
  279.     TCall            sndcall, rcvcall;
  280.     OSStatus        err = kOTNoError;
  281.     long            myport = 0;
  282.     InetHost        myaddr = 0;
  283.     char            mystr[255];
  284.     long            bytes = 0;
  285.     OTFlags            flags = 0;
  286.  
  287.     myport = 0;
  288.     fprintf(stderr, "Pitch port ? (enter TCP port number)\n");
  289.     if ( gets(mystr) != 0 )
  290.     {
  291.         stringtonum(mystr, &myport);
  292.         gPitchPort =(InetPort)  myport;
  293.     }
  294.     myaddr = 0;
  295.     fprintf(stderr, "Pitch IP address ? (enter IP address)\n");
  296.     if ( gets(mystr) != 0 )
  297.     {
  298.         if (OTInetStringToHost(mystr, &myaddr) == 0)
  299.         {
  300.             gPitchIpAddr = (InetHost) myaddr;
  301.         }
  302.     }
  303.     myport = 0;
  304.     fprintf(stderr, "Catch port ? (enter port number)\n");
  305.     if ( gets(mystr) != 0 )
  306.     {
  307.         stringtonum(mystr, &myport);
  308.         gCatchPort = (InetPort) myport;
  309.     }
  310.     myaddr = 0;
  311.     fprintf(stderr, "Catch IP address ? (enter IP address)\n");
  312.     if ( gets(mystr) != 0 )
  313.     {
  314.         if ( OTInetStringToHost(mystr, &myaddr) == 0 ) 
  315.         {
  316.             gCatchIpAddr = (InetHost) myaddr;
  317.         }
  318.     }
  319.  
  320.     fprintf(stderr, "What should I send ? (enter data string)\n");
  321.     if ( gets(data) == 0 )
  322.     {
  323.         strcpy(data, defaultData);
  324.         fprintf(stderr, "sending default data: <%s>\n", data);
  325.     }
  326.     data[strlen(data)] = '\n';
  327.  
  328.     OTInetHostToString(gCatchIpAddr, mystr);
  329.     fprintf(stderr, "The program will send a packet to <%s:%d> on port <%d>\n", mystr, gCatchPort, gPitchPort);
  330.  
  331.     memset(&sndsin, 0, sizeof(struct InetAddress));
  332.     memset(&rcvsin, 0, sizeof(struct InetAddress));
  333.     memset(&sndcall, 0, sizeof(TCall));
  334.     memset(&rcvcall, 0, sizeof(TCall));
  335.     memset(&reqsin, 0, sizeof(struct InetAddress));
  336.     memset(&rcvsin, 0, sizeof(struct InetAddress));
  337.     memset(&req, 0, sizeof(TBind));
  338.     memset(&ret, 0, sizeof(TBind));
  339.  
  340.     OTInitInetAddress(&sndsin, gCatchPort, gCatchIpAddr);
  341.  
  342.     do
  343.     {
  344.         //
  345.         // Now create a TCP
  346.         //
  347.         ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, &info, &err);
  348.  
  349.         if ( ep == NULL || err != kOTNoError )
  350.         {
  351.             ep = NULL;
  352.             fprintf(stderr,"ERROR: OpenEndpoint(\"TCP\") failed with %d\n", err);
  353.             break;
  354.         }
  355.  
  356.         err = ep->SetSynchronous();
  357.         if ( err != kOTNoError )
  358.         {
  359.             fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
  360.             break;
  361.         }
  362.  
  363.         //
  364.         // Install notifier we're going to use for testing
  365.         //
  366.         err = ep->InstallNotifier(&EventHandler, 0);
  367.         if ( err != kOTNoError )
  368.         {
  369.             fprintf(stderr, "ERROR: InstallNotifier() failed with %d\n", err);
  370.             break;
  371.         }
  372.  
  373.         //
  374.         // Try to bind
  375.         // 
  376.         OTInitInetAddress(&reqsin, gPitchPort, gPitchIpAddr);
  377.  
  378.         req.addr.len = sizeof(struct InetAddress);
  379.         req.addr.buf = (unsigned char *) &reqsin;
  380.         req.qlen = 2;                                        // don't care for tcp
  381.         ret.addr.maxlen = sizeof(struct InetAddress);
  382.         ret.addr.buf = (unsigned char *) &retsin;
  383.  
  384.         // bind TCP to current address and port
  385.         err = ep->Bind(&req, &ret);
  386.         if ( err != kOTNoError )
  387.         {
  388.             fprintf(stderr, "ERROR: Bind() failed with %d\n", err);
  389.             break;
  390.         }
  391.         err = ep->SetSynchronous();
  392.         if ( err != kOTNoError )
  393.         {
  394.             fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
  395.             break;
  396.         }
  397.  
  398.         ShowEndpointOptions(ep);
  399.  
  400.         sndcall.addr.len = sizeof(struct InetAddress);
  401.         sndcall.addr.buf = (unsigned char *) &sndsin;
  402.     
  403.         rcvcall.addr.maxlen = sizeof(struct InetAddress);
  404.         rcvcall.addr.buf = (unsigned char *) &rcvsin;
  405.  
  406.         err = ep->Connect(&sndcall, &rcvcall);
  407.         if ( err != kOTNoError )
  408.         {
  409.             fprintf(stderr, "ERROR: Connect() failed with %d\n", err);
  410.             break;
  411.         }
  412.  
  413.         bytes = ep->Snd(data, strlen(data), flags);
  414.         if ( bytes >= 0 )
  415.         {
  416.             fprintf(stderr, "Sent packet\nbytes: <%d> data: %s", bytes, data);
  417.         }
  418.         else 
  419.         {
  420.             fprintf(stderr, "ERROR: Snd() failed with %d\n", bytes);
  421.         }
  422.         Idle();
  423.     } while (false);
  424.  
  425.     if ( ep != NULL )
  426.     {
  427.         err = ep->SndOrderlyDisconnect();
  428.         if ( err != kOTNoError )
  429.         {
  430.             if ( err == kOTLookErr )
  431.                 fprintf(stderr, "SndOrderlyDisconnect() returns %d\n", err);
  432.             else
  433.                 fprintf(stderr, "ERROR: SndOrderlyDisconnect() failed with %d\n", err);
  434.         }
  435.     
  436.         while (gCallRcvOrdDiscon == 0)
  437.             Idle();
  438.         err = ep->RcvOrderlyDisconnect();
  439.         if ( err != kOTNoError )
  440.         {
  441.             fprintf(stderr, "ERROR: RcvOrderlyDisconnect() failed with %d\n", err);
  442.         }
  443.     
  444.         //
  445.         // Remove notifier
  446.         //
  447.         ep->RemoveNotifier();    
  448.         //
  449.         // Try to Unbind
  450.         // 
  451.     /*
  452.         err = ep->Unbind();
  453.         if ( err != kOTNoError )
  454.         {
  455.             fprintf(stderr, "ERROR: Unbind() returned %d\n", err);
  456.         }
  457.     */
  458.         //
  459.         // Get rid of endpoint.
  460.         //
  461.         err = OTCloseProvider(ep);
  462.         if ( err != kOTNoError )
  463.         {
  464.             fprintf(stderr, "ERROR: CloseEndpoint() failed with %d\n", err);
  465.         }
  466.     }
  467.     fprintf(stderr, "Bye\n");
  468. }
  469.